package Pre.Work;

import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.TreeSet;

public class Exam03 {
    //创建一个HashSet，包含一些单词。编写代码，找到集合中最长的单词。
    public static void main(String[] args) {
        HashSet<String> hashSet = new HashSet();
        hashSet.add("different");
        hashSet.add("apple");
        hashSet.add("add");
        hashSet.add("foreach");
        hashSet.add("playcomputergame");
        hashSet.add("football");
        hashSet.add("difference");
        hashSet.add("beautiful");

        String longer = "";

        Iterator iterator = hashSet.iterator();
        while (iterator.hasNext()) {
            String str = (String) iterator.next();
            if (str.length() > longer.length()) {
                longer = str;
            }
        }
        System.out.println("最长的单词为：" + longer);


        TreeSet<String> treeSet = new TreeSet<>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                if (o1.equals(o2)) {
                    return 0;
                }
                return o1.length() - o2.length() > 0 ? 1 : -1;
            }
        });
        treeSet.addAll(hashSet);
        System.out.println(treeSet.last());
    }
}
